<template>
    <div>
<el-card>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
        <el-form-item label="店铺" prop="shopsName">
          <el-input
            v-model="queryParams.shopsName"
            placeholder="请输入店铺名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input
            v-model="queryParams.address"
            placeholder="请输入地址"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="类型" prop="categoryType">
          <el-select v-model="queryParams.categoryType" placeholder="请选择人员状态" clearable size="small">
            <el-option label="表示建房主材" value="0"/>
            <el-option label="表示家居建材" value="1"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品ID" align="center" prop="productId" />
<!--        <el-table-column label="商品编码" align="center" prop="productCore" />-->
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="顺序" align="center" prop="barCode" />
        <el-table-column label="地区" align="center" prop="address" />
        <el-table-column label="店铺名称" align="center" prop="shopsName" />


<!--        <el-table-column label="商品的供应商ID" align="center" prop="supplierId" />-->
<!--        <el-table-column label="商品的分类ID" align="center" prop="categoryId" />-->
        <el-table-column label="商品销售价格" align="center" prop="price" />
<!--        <el-table-column label="商品有效期" align="center" prop="shelfLife" />-->

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />


      <!-- 添加或修改商品信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="序号" prop="barCode">
            <el-input v-model="form.barCode" placeholder="请输入国条码" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
</el-card>
    </div>
</template>

<script>
  import { dianpuedit,dianpuid,dianpuList } from "../../../api/sequence";

  export default {
        name: "index",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品信息表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productCore: null,
          productName: null,
          barCode: null,
          brandId: null,
          oneCategoryId: null,
          twoCategoryId: null,
          threeCategoryId: null,
          supplierId: null,
          categoryId: null,
          price: null,
          averageCost: null,
          publishStatus: null,
          auditStatus: null,
          weight: null,
          length: null,
          height: null,
          width: null,
          colorType: null,
          productionDate: null,
          shelfLife: null,
          descript: null,
          indate: null,
          modifiedTime: null,
          cornerQuantity: null,
          labelName: null,
          salesQuantity: null,
          groupType: null,
          userId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          productCore: [
            { required: true, message: "商品编码不能为空", trigger: "blur" }
          ],
          productName: [
            { required: true, message: "商品名称不能为空", trigger: "blur" }
          ],
          brandId: [
            { required: true, message: "品牌表的ID不能为空", trigger: "blur" }
          ],
          supplierId: [
            { required: true, message: "商品的供应商ID不能为空", trigger: "blur" }
          ],
          categoryId: [
            { required: true, message: "商品的分类ID不能为空", trigger: "blur" }
          ],
          price: [
            { required: true, message: "商品销售价格不能为空", trigger: "blur" }
          ],
          averageCost: [
            { required: true, message: "商品加权平均成本不能为空", trigger: "blur" }
          ],
          publishStatus: [
            { required: true, message: "上下架状态：1下架0上架不能为空", trigger: "blur" }
          ],
          auditStatus: [
            { required: true, message: "审核状态：1未审核，0已审核不能为空", trigger: "blur" }
          ],
          productionDate: [
            { required: true, message: "生产日期不能为空", trigger: "blur" }
          ],
          shelfLife: [
            { required: true, message: "商品有效期不能为空", trigger: "blur" }
          ],
          descript: [
            { required: true, message: "商品描述不能为空", trigger: "blur" }
          ],
          indate: [
            { required: true, message: "商品录入时间不能为空", trigger: "blur" }
          ],
          modifiedTime: [
            { required: true, message: "最后修改时间不能为空", trigger: "blur" }
          ],
          delFlag: [
            { required: true, message: "删除标识：0显示 2屏蔽不能为空", trigger: "blur" }
          ],
          groupType: [
            { required: true, message: "是否是拼团:0不拼团  2拼团不能为空", trigger: "change" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询商品信息列表 */
      getList() {
        this.loading = true;
        dianpuList(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },


      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          productId: null,
          productCore: null,
          productName: null,
          barCode: null,
          brandId: null,
          oneCategoryId: null,
          twoCategoryId: null,
          threeCategoryId: null,
          supplierId: null,
          categoryId: null,
          price: null,
          averageCost: null,
          publishStatus: 0,
          auditStatus: 0,
          weight: null,
          length: null,
          height: null,
          width: null,
          colorType: null,
          productionDate: null,
          shelfLife: null,
          descript: null,
          indate: null,
          modifiedTime: null,
          cornerQuantity: null,
          labelName: null,
          salesQuantity: null,
          delFlag: null,
          groupType: null,
          userId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.productId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const productId = row.productId || this.ids
        dianpuid(productId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改商品信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.productId != null) {
              const data = {productId:this.form.productId,barCode:this.form.barCode}
              dianpuedit(data).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              })
            } else {
              addInfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },


    }



    }
</script>

<style scoped>

</style>
